//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.app.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.base.domain.*;
import com.ruoyi.live.base.mapper.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

@Service
public class CacheService {
    private static final Logger log = LoggerFactory.getLogger(CacheService.class);
    @Autowired
    LiveBarrageMapper barrageDao;
    @Autowired
    LiveExamTaskMapper letDao;
    @Autowired
    LiveTaskUserChannelMapper channelDao;
    @Autowired
    SystemParamMapper paramDao;
    @Autowired
    TaskCatalogMapper catalogDao;
    @Autowired
    TaskMapper taskDao;
    @Autowired
    TaskSourceMapper sourceDao;
    @Autowired
    TaskModuleMapper moduleDao;
    @Autowired
    LiveProductMapper productDao;
    @Autowired
    LiveExamMapper examDao;
    @Autowired
    LiveScoreStandardMapper standardDao;
    @Autowired
    LiveProductSourceMapper productSourceMapper;

    public CacheService() {
    }

    @Cacheable({"getProductNameById"})
    public String getProductNameById(Long id) {
        return ((LiveProduct)this.productDao.selectById(id)).getProductName();
    }

    @CacheEvict(
            value = {"paramCache"},
            key = "#code"
    )
    public void clearParamCache(String code) {
    }

    @Cacheable({"expertLiveExamById"})
    public LiveExam getLiveExam(Long id) {
        LiveExam exam = (LiveExam)this.examDao.selectById(id);
        List<LiveExamTask> list = this.letDao.selectList(new QueryWrapper((new LiveExamTask()).setExamId(exam.getId())));
        Iterator var4 = list.iterator();

        while(var4.hasNext()) {
            LiveExamTask task = (LiveExamTask)var4.next();
            task.setProductName(((LiveProduct)this.productDao.selectById(task.getProductId())).getProductName());
        }

        exam.setTasks(list);
        return exam;
    }

    @Cacheable({"customBarrageCache"})
    public List<LiveBarrage> customBarrageList() {
        return this.barrageDao.selectList((Wrapper)(new QueryWrapper()).in("type", new Object[]{1, 2}));
    }

    @Cacheable(
            value = {"giftBarrageByIdCache"},
            key = "#id"
    )
    public LiveBarrage giftBarrageByIdCache(Long id) {
        return (LiveBarrage)this.barrageDao.selectById(id);
    }

    @Cacheable({"giftBarrageCache"})
    public List<LiveBarrage> giftBarrageList() {
        return this.barrageDao.selectList(new QueryWrapper((new LiveBarrage()).setType(3)));
    }

    @Cacheable(
            value = {"questionBarrageCache"},
            key = "#productId"
    )
    public List<LiveBarrage> questionBarrageList(Long productId) {
        return this.barrageDao.selectList((Wrapper)(new QueryWrapper((new LiveBarrage()).setType(5).setProductId(productId))).orderByAsc("orders"));
    }

    @Cacheable({"questionBarrageByIdCache"})
    public List<LiveBarrage> questionBarrageByIdCache(Long productId) {
        return this.barrageDao.selectList((Wrapper)(new QueryWrapper((new LiveBarrage()).setType(5).setProductId(productId))).orderByAsc("orders"));
    }

    @Cacheable(
            value = {"examCache"},
            key = "#id"
    )
    public LiveExamTask getExamByChannelId(Long id) {
        LiveTaskUserChannel channel = (LiveTaskUserChannel)this.channelDao.selectById(id);
        return (LiveExamTask)this.letDao.selectById(channel.getTaskId());
    }

    @CacheEvict({"customBarrageCache"})
    public void evictCBarrageCache() {
    }

    @CacheEvict({"questionBarrageCache"})
    public void evictQBarrageCache() {
    }

    @CacheEvict({"giftBarrageCache"})
    public void evictBarrageCache() {
    }

    @Cacheable(
            value = {"barrageCache"},
            key = "#id"
    )
    public LiveBarrage getOneBarrage(Long id) {
        return (LiveBarrage)this.barrageDao.selectById(id);
    }

    @Cacheable(
            value = {"paramCache"},
            key = "#code"
    )
    public String getParam(String code) {
        List<SystemParam> list = this.paramDao.selectList(new QueryWrapper((new SystemParam()).setParamCode(code)));
        return list.size() == 0 ? null : ((SystemParam)list.get(0)).getParamValue();
    }

    @Cacheable({"taskCache"})
    public List<TaskCatalog> getTasks() {
        List<TaskCatalog> list = this.catalogDao.selectList((Wrapper)null);
        List<Task> tasks = this.taskDao.selectList((Wrapper)(new QueryWrapper((new Task()).setState(1))).orderByAsc("orders"));
        Map<Long, TaskCatalog> map = (Map)list.stream().collect(Collectors.toMap(TaskCatalog::getId, (cata) -> {
            return cata;
        }));
        Iterator var4 = tasks.iterator();

        while(var4.hasNext()) {
            Task task = (Task)var4.next();
            List<Task> ts = ((TaskCatalog)map.get(task.getCatalogId())).getTasks();
            if (ts == null) {
                ts = new ArrayList();
                ts.add(task);
                ((TaskCatalog)map.get(task.getCatalogId())).setTasks(ts);
            } else {
                ts.add(task);
            }
        }

        List<TaskCatalog> res = new ArrayList();
        Iterator var9 = map.entrySet().iterator();

        while(var9.hasNext()) {
            Entry<Long, TaskCatalog> entry = (Entry)var9.next();
            TaskCatalog mt = (TaskCatalog)entry.getValue();
            if (mt.getTasks() != null) {
                mt.setCount(mt.getTasks().size());
                res.add(mt);
            }
        }

        return res;
    }

    @Cacheable(
            value = {"taskDetailCache"},
            key = "#id"
    )
    public Task getTaskDetail(Long id) {
        Task task = (Task)this.taskDao.selectById(id);
        List<TaskSource> list = this.sourceDao.selectList(new QueryWrapper((new TaskSource()).setTaskId(id).setState(1)));
        List<TaskSource> sources = new ArrayList();
        task.setSources(sources);
        Iterator var5 = list.iterator();

        while(var5.hasNext()) {
            TaskSource sou = (TaskSource)var5.next();
            if (sou.getType() == 1) {
                task.setExample(sou);
            } else if (sou.getType() == 2) {
                task.getSources().add(sou);
            } else if (sou.getType() == 3) {
                task.setGuided(sou);
            }
        }

        List<TaskModule> modules = this.moduleDao.selectList((Wrapper)(new QueryWrapper((new TaskModule()).setState(1).setTaskId(id))).orderByAsc("orders"));
        task.setModules(modules);
        return task;
    }

    public List<LiveScoreStandard> getStandards(Integer type) {
        List<LiveScoreStandard> parents = this.standardDao.selectList((Wrapper)(new QueryWrapper((new LiveScoreStandard()).setParentId(0L).setStandardType(2).setType(type))).orderByAsc("orders"));
        Iterator var3 = parents.iterator();

        while(var3.hasNext()) {
            LiveScoreStandard standard = (LiveScoreStandard)var3.next();
            standard.setStandards(this.standardDao.selectList((Wrapper)(new QueryWrapper((new LiveScoreStandard()).setParentId(standard.getId()))).orderByAsc("orders")));
        }

        return parents;
    }

    @CacheEvict(
            cacheNames = {"getScoreStandardById", "getScoreItemCount", "getProductNameById", "expertLiveExamById", "liveScoreStandardCache", "taskDetailCache", "taskCache", "paramCache", "barrageCache", "giftBarrageCache", "questionBarrageCache", "customBarrageCache", "examCache", "giftBarrageByIdCache", "standardByCode", "getProductSourceByProductId"},
            allEntries = true
    )
    @PreAuthorize("@ss.hasAnyRoles('admin')")
    public void doEvit() {
        log.error("{}清除所有缓存", System.currentTimeMillis());
    }

    @Cacheable(
            value = {"standardByCode"},
            key = "#code"
    )
    public LiveScoreStandard getStandardByCode(String code) {
        List<LiveScoreStandard> list = this.standardDao.selectList(new QueryWrapper((new LiveScoreStandard()).setStandardCode(code)));
        return list.size() > 0 ? (LiveScoreStandard)list.get(0) : null;
    }

    @Cacheable(
            value = {"getScoreStandardById"},
            key = "#id"
    )
    public LiveScoreStandard getScoreItemById(Long id) {
        LiveScoreStandard standard = (LiveScoreStandard)this.standardDao.selectById(id);
        if (standard.getScoreType() == 3) {
            String[] scoresStr = standard.getStandardValueJson().split(",");
            List<BigDecimal> scores = new ArrayList();
            String[] var5 = scoresStr;
            int var6 = scoresStr.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                String scoreStr = var5[var7];
                scores.add(new BigDecimal(scoreStr));
            }

            standard.setScores(scores);
        }

        return standard;
    }

    @Cacheable(
            value = {"getAllSourceByProductId"},
            key = "#id"
    )
    public Map<Integer, List<Long>> getAllSourceByProductId(Long id) {
        List<LiveProductSource> sources = this.productSourceMapper.selectList(new QueryWrapper((new LiveProductSource()).setSourceType(2).setProductId(id)));
        Map<Integer, List<Long>> res = new HashMap();
        Iterator var4 = sources.iterator();

        while(var4.hasNext()) {
            LiveProductSource source = (LiveProductSource)var4.next();
            List<Long> longs = (List)res.get(source.getSection());
            if (longs == null) {
                longs = new ArrayList();
                longs.add(source.getId());
                res.put(source.getSection(), longs);
            } else {
                longs.add(source.getId());
            }
        }

        return res;
    }

    @Cacheable({"getScoreItemCount"})
    public Integer getScoreItemCount() {
        Long sigleCount = this.standardDao.selectCount((Wrapper)(new QueryWrapper((new LiveScoreStandard()).setStandardType(2).setScoreScope(1))).ne("parent_id", 0L));
        Long doubleCount = this.standardDao.selectCount((Wrapper)(new QueryWrapper((new LiveScoreStandard()).setStandardType(2).setScoreScope(2))).ne("parent_id", 0L));
        return sigleCount.intValue() + doubleCount.intValue() * 2;
    }

    @Cacheable(
            value = {"getProductSourceByProductId"},
            key = "#id"
    )
    public Map<Integer, List<Long>> getProductSourceByProductId(Long id) {
        List<LiveProductSource> sources = this.productSourceMapper.selectList(new QueryWrapper((new LiveProductSource()).setAnswer(1).setSourceType(2).setProductId(id)));
        Map<Integer, List<Long>> res = new HashMap();
        Iterator var4 = sources.iterator();

        while(var4.hasNext()) {
            LiveProductSource source = (LiveProductSource)var4.next();
            List<Long> longs = (List)res.get(source.getSection());
            if (longs == null) {
                longs = new ArrayList();
                longs.add(source.getId());
                res.put(source.getSection(), longs);
            } else {
                longs.add(source.getId());
            }
        }

        return res;
    }
}
